package com.bwie.car.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * @Author：杨鹏
 * @Package：com.bwie.car.consumer
 * @Project：yp_zhoukao723
 * @name：CarConsumer
 * @Date：2024/7/23 13:08
 */
@Log4j2
@Component
public class CarConsumer {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String INDEX_MQ = "index_mq";

    @RabbitListener(queuesToDeclare = {@Queue("yp_723_mq")})
    public void carConsumer(String userName, Message message , Channel channel){

        String messageId = message.getMessageProperties().getMessageId();


        Long add = redisTemplate.opsForSet().add(INDEX_MQ, messageId);
        if (add == 1){
            log.info("mq开始消费");

            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException e) {
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
                } catch (IOException ex) {
                    log.info("mq消费报错");
                }
                throw new RuntimeException(e);
            }
        }

    }

}
